package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.TrainPlan;
import cn.wolfcode.crm.mapper.TrainPlanMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ITrainPlanService;
import cn.wolfcode.crm.util.LogicException;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 30821 on 2018/7/11.
 */
@Service
public class TrainPlanServiceImpl implements ITrainPlanService{
    @Autowired
    private TrainPlanMapper mapper;

    public void save(TrainPlan entity) {
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        entity.setEmployee(employee);
        entity.setStatus(0);
        if (mapper.insert(entity) < 1) {
            throw new LogicException("添加失败");
        }
    }

    public void delete(Long id) {
        if (mapper.deleteByPrimaryKey(id) < 1) {
            throw new LogicException("删除失败");
        }
    }

    public void update(TrainPlan entity) {

        if (mapper.updateByPrimaryKey(entity) < 1) {
            throw new LogicException("修改失败");
        }
    }

    public TrainPlan getById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<TrainPlan> listAll() {
        return mapper.selectAll();
    }

    public PageResult queryPage(QueryObject qo) {
        PageResult page = new PageResult();
        int count = mapper.queryCount(qo);
        if (count == 0) {
            return page;
        }
        List<TrainPlan> list = mapper.queryPageData(qo);
        page.setRows(list);
        page.setTotal(count);
        return page;
    }



}
